
/*
 *  经过分析cell是由三部分组成的:
 *  1,原发视图
 *  2,转发视图
 *  3,toolbar视图
 */

import UIKit
import SnapKit


class HRHomePageTableViewCell: UITableViewCell {
    //  定义一个约束的属性,用来记录要改变的约束
    var toolBarViewTopContraint: Constraint?//    用这个属性前要导入SnapKit
    var indexPath: NSIndexPath?{//接收cell的indexPath
        didSet{
            print(indexPath?.row)
            toolBarView.index = (indexPath?.row)!
        }
    }
    var homePageStatusViewModel:HRHomePageStatusViewModel?{
        didSet{
            //  将模型赋值给原创微博视图
            originView.homePageStatusViewModel = homePageStatusViewModel
           
            //  将模型数据传递给toolbar视图
            toolBarView.homePageStatusViewModel = homePageStatusViewModel
            //  判断是否有转发微博,来决定cell的高度
            
            //  if语句里面有修改约束,所以先把以前的约束卸载掉
            self.toolBarViewTopContraint?.uninstall()
            
            if homePageStatusViewModel?.homePageModel?.retweeted_status != nil{//  有转发微博的情况
                
                //  将模型赋值给转发视图
                retweeted_status.homePageStatusViewModel = homePageStatusViewModel
                //  更新toolBarView 的约束
                toolBarView.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarViewTopContraint = make.top.equalTo(retweeted_status.snp_bottom).constraint
                })
                //   显示转发微博视图
                retweeted_status.hidden = false
                
            }else{
                //  没有转发微博的情况
                toolBarView.snp_updateConstraints(closure: { (make) -> Void in
                   self.toolBarViewTopContraint = make.top.equalTo(originView.snp_bottom).constraint
                })
                //    隐藏转发微博视图
                retweeted_status.hidden = true
            }
        }
    }
    
    //  懒加载原发部分的视图
    private lazy var originView: HROriginWeiBoView = HROriginWeiBoView()
    //  懒加载toolbar
     private lazy var toolBarView: HRHomePageToolBar = HRHomePageToolBar()
    //  懒加载转发微博
    private lazy var retweeted_status: HRRetweetView = HRRetweetView()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

     // MARK: - 添加控件,设置约束
    private func setupUI(){
        contentView.backgroundColor = UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1)
        originView.backgroundColor = UIColor.whiteColor()
        retweeted_status.backgroundColor = UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1)
        contentView.addSubview(originView)
        contentView.addSubview(toolBarView)
        contentView.addSubview(retweeted_status)
        
//        toolBarView.tag = (indexPath?.row)!
        
        originView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView).offset(5)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            // 原创微博视图的高度暂定为200
//            make.height.equalTo(150)
        }
        retweeted_status.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentView)
            make.top.equalTo(originView.snp_bottom)
            make.trailing.equalTo(contentView)
            
        }
       
        toolBarView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            make.height.equalTo(35)
            //  暂定的约束,后面还有根据是否有转发进行更改
          self.toolBarViewTopContraint = make.top.equalTo(retweeted_status.snp_bottom).constraint
            //  toolbar高度暂定
        }
        
        //  关键约束:contentView
        contentView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(toolBarView)
        }
    }

}
